<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Artist extends CI_Controller
{
	public function __construct()
	{
        parent::__construct();
		$this->_module_name = $this->router->fetch_module().'/';
		$this->pre_message = "" ;
		$this->load->model('artist_model','artist');
		$this->load->model('banner_model','banner');
		$this->load->config('config_site') ;
		$this->lang->load('adv') ;
		$this->lang->load('banner');
		/*$this->permit_library->check_permit();
	  	$this->session->set_userdata(array('Url'=>uri_string()));*/   
	}

	function index($start=0, $banner_type='ARTIST')
	{
		$data['start'] = $start;
		$data['banner_type'] = $banner_type;
		$data['title'] = 'Quản lý artist';
		$data['add'] = $this->_module_name.'artist/add';
		$config['base_url'] = base_url().$this->_module_name.'artist/index/'.$start;  
		$config['total_rows']   =  $this->artist->get_num($banner_type);
		$data['num'] = $config['total_rows'];
		$paging_limit = $this->config->item('paging_limit');
		$config['per_page']  =  20 ;
		$config['uri_segment'] = 4; 
		$this->pagination->initialize($config);
		$data['list'] =   $this->artist->get_all($config['per_page'],$start, $banner_type);
		$data['pagination']    = $this->pagination->create_links();
		
		$data['page'] = 'artist/index';
		$this->load->view('templates/skin', $data);
	}

	function add()
	{
		$data['title'] = 'Thêm mới quảng cáo';
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('lb_image',lang('adv_images'),'trim|required');
			//$this->form_validation->set_rules('nb_position',lang('position'),'trim|required');
			if($this->form_validation->run()== FALSE){
				$this->pre_message = validation_errors();
			}else{
				$data =  $this-> build_data($_POST, 1); 
				if($this->artist->save($data))
				{
					$this->session->set_flashdata('message',lang('successful'));
					redirect($this->_module_name.'artist');
				}
			}
		}
		$data['list_banner_type']=$this->banner->getAllBannerType();
		$data['message'] = $this->pre_message;
		$data['page'] = 'artist/add';
		$this->load->view('templates/skin', $data);
	}  
	
	function edit($id)
	{
		
		$data['rs'] = $this->artist->get_id($id);
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('lb_url','','trim|required');
			//$this->form_validation->set_rules('nb_position',lang('position'),'trim|required');
			if($this->form_validation->run()== FALSE){
				$this->pre_message = validation_errors();
			}else{
				$data =  $this-> build_data($_POST); 
				if($this->artist->save($data, $id))
				{
					$this->session->set_flashdata('message',lang('successful'));
					redirect($this->_module_name.'artist');
				}
			}
		}
		$data['list_banner_type']=$this->banner->getAllBannerType();
		$data['title'] = lang('update');
		$data['page'] = 'artist/edit';
		$this->load->view('templates/skin', $data);
	}

	function build_data($data, $flag=0)
	{
		$this->load->helper('img') ;
		//upload hinh anh
		$img = $this->input->post("lb_image");
		$img_name = end(explode('/', $img)) ;
		if($img)
		{
			if(file_exists('../uploads/banner/artist/'.$img_name))
			{
				unlink('../uploads/banner/artist/'.$img_name);
			}
			fn_resize_image('../uploads/'.$img,'../uploads/banner/artist/'.$img_name, 270, 180);
			$req["lb_image"] = $img_name;
		}
		//upload hinh anh lớn
		$img_large = $this->input->post("lb_image_large");
		$img_name_large = end(explode('/', $img_large)) ;
		if($img_large)
		{
			if(file_exists('../uploads/banner/'.$img_name_large))
			{
				unlink('../uploads/banner/'.$img_name_large);
			}
			fn_resize_image('../uploads/'.$img_large,'../uploads/banner/'.$img_name_large, 1366, 440); 
			$req["lb_image_large"] = $img_name_large;
		}
		
		$req["lb_title"]=trim($data["lb_title"]);
		$req["banner_type"]= trim($data["banner_type"]);
		$req["lb_url"]=trim($data["lb_url"]);
		$req["lb_sumary"]=trim($data["lb_sumary"]);
		$req["lb_desc"]=trim($data["lb_desc"]);
		$req["nb_position"] = trim($data["nb_position"]);
		$req["nb_order"]=trim($data["nb_order"]);
		$req["bl_active"]= (int) $this->input->post("bl_active");
		$req["dt_modify"]= date("Y-m-d H:i:s");
		if($flag) $req["dt_create"]= date("Y-m-d H:i:s");
		return $req;
	}
	
	function del($id=0, $page=1)
	{
		//$id = $this->uri->segment(3);
		//$page = $this->uri->segment(4);
		if($this->artist->del($id))
			$this->session->set_flashdata('message',lang('delete_success'));
		else $this->session->set_flashdata('message',lang('delete_unsuccess'));
		
		// xóa các tin đã bo sau 3 tháng
		$this->artist->remove() ;
		redirect($this->_module_name.'artist');
	}
	
	function dels()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
					
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->artist->del($ar_id[$i]))
							$this->session->set_flashdata('message',lang('delete_success'));
						else $this->session->set_flashdata('message',lang('delete_unsuccess'));
					}
				}
			}
		}
		redirect($this->_module_name.'artist');
	}  
}

?>